﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_Hotel_Management_System.Class;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class Housekeeping_and_Staff_Report : Form
    {
        public Housekeeping_and_Staff_Report()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (LogInClass.position == "Administrator")
            {
                Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                this.Hide();
                mainA.Show();
            }
            else
            {
                Main__Receptionist_ mainR = new Main__Receptionist_();
                this.Hide();
                mainR.Show();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
        }

        DateTime startingDate;
        DateTime endingDate;
       
        private void btnGenerateDailyReport_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.Equals("Daily"))
            {
                label1.Visible = true;
                dateTimePicker1.Visible = true;
                label2.Visible = true;
                dateTimePicker2.Visible = true;
                endingDate = Convert.ToDateTime(dateTimePicker2.Value.Date.Year + "/" + dateTimePicker2.Value.Date.Month + "/" + dateTimePicker2.Value.Date.Day);
                startingDate = Convert.ToDateTime(dateTimePicker1.Value.Date.Year + "/" + dateTimePicker1.Value.Date.Month + "/" + dateTimePicker1.Value.Date.Day);

                List<HouseKeeping> HKList = DBManager.getHouseKeepingDate(startingDate, endingDate);

                dataGridView1.DataSource = HKList;
                
                if (HKList.Count == 0)
                {
                    MessageBox.Show("No record found!");
                }
            }
            else if (comboBox1.SelectedItem.Equals("Weekly"))
            {
                startingDate = Convert.ToDateTime(dateTimePicker1.Value.Date.Year + "/" + dateTimePicker1.Value.Date.Month + "/" + dateTimePicker1.Value.Date.Day);
                endingDate = Convert.ToDateTime(dateTimePicker1.Value.Date.Year + "/" + dateTimePicker1.Value.Date.Month + "/" + dateTimePicker1.Value.Date.Day).AddDays(7);

                List<HouseKeeping> HKList = DBManager.getHouseKeepingDate(startingDate, endingDate);

                dataGridView1.DataSource = HKList;

                if (HKList.Count == 0)
                {
                    MessageBox.Show("No record found!");
                }
            }
            else if (comboBox1.SelectedItem.Equals("Monthly"))
            {
                if (comboBox2.SelectedItem.Equals("January"))
                {
                    int month = 1;
                    List<HouseKeeping> HKList1 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList1;

                    if (HKList1.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("February"))
                {
                    int month = 2;
                    List<HouseKeeping> HKList2 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList2;

                    if (HKList2.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("March"))
                {
                    int month = 3;
                    List<HouseKeeping> HKList3 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList3;

                    if (HKList3.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("April"))
                {
                    int month = 4;
                    List<HouseKeeping> HKList4 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList4;

                    if (HKList4.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("May"))
                {
                    int month = 5;
                    List<HouseKeeping> HKList5 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList5;

                    if (HKList5.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("June"))
                {
                    int month = 6;
                    List<HouseKeeping> HKList6 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList6;

                    if (HKList6.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("July"))
                {
                    int month = 7;
                    List<HouseKeeping> HKList7 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList7;

                    if (HKList7.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("August"))
                {
                    int month = 8;
                    List<HouseKeeping> HKList8 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList8;

                    if (HKList8.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("September"))
                {
                    int month = 9;
                    List<HouseKeeping> HKList9 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList9;

                    if (HKList9.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("October"))
                {
                    int month = 10;
                    List<HouseKeeping> HKList10 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList10;

                    if (HKList10.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("November"))
                {
                    int month = 11;
                    List<HouseKeeping> HKList11 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList11;

                    if (HKList11.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
                else if (comboBox2.SelectedItem.Equals("December"))
                {
                    int month = 12;
                    List<HouseKeeping> HKList12 = DBManager.getHouseKeepingMonth(month);

                    dataGridView1.DataSource = HKList12;

                    if (HKList12.Count == 0)
                    {
                        MessageBox.Show("No record found!");
                    }
                }
            }
            else if (comboBox1.SelectedItem.Equals("Yearly"))
            {
                int year = Convert.ToInt32(tbxYear.Text);
                List<HouseKeeping> HKListYear = DBManager.getHouseKeepingYear(year);

                dataGridView1.DataSource = HKListYear;

                tbxYear.Clear();

                if (HKListYear.Count == 0)
                {
                    MessageBox.Show("No record found!");
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.Equals("Daily"))
            {
                label1.Visible = true;
                dateTimePicker1.Visible = true;
                label2.Visible = true;
                dateTimePicker2.Visible = true;

                comboBox2.Visible = false;
                label4.Visible = false;
                label5.Visible = false;
                tbxYear.Visible = false;
            }
            else if (comboBox1.SelectedItem.Equals("Weekly"))
            {
                label1.Visible = true;
                dateTimePicker1.Visible = true;

                label2.Visible = false;
                dateTimePicker2.Visible = false;
                label4.Visible = false;
                comboBox2.Visible = false;
                label5.Visible = false;
                tbxYear.Visible = false;
            }
            else if (comboBox1.SelectedItem.Equals("Monthly"))
            {
                label4.Visible = true;
                comboBox2.Visible = true;

                label1.Visible = false;
                dateTimePicker1.Visible = false;
                label2.Visible = false;
                dateTimePicker2.Visible = false;
                label5.Visible = false;
                tbxYear.Visible = false;
            }
            else if (comboBox1.SelectedItem.Equals("Yearly"))
            {
                label5.Visible = true;
                tbxYear.Visible = true;
                
                label1.Visible = false;
                dateTimePicker1.Visible = false;
                label2.Visible = false;
                dateTimePicker2.Visible = false;
                label4.Visible = false;
                comboBox2.Visible = false;
            }
        }

        private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            ExcelApp.Application.Workbooks.Add(Type.Missing);
            ExcelApp.Columns.ColumnWidth = 10;

            ExcelApp.Cells[1, 1] = dataGridView1.Columns[0].HeaderText;
            ExcelApp.Cells[1, 2] = dataGridView1.Columns[1].HeaderText;
            ExcelApp.Cells[1, 3] = dataGridView1.Columns[2].HeaderText;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                ExcelApp.Cells[i + 2, 1] = dataGridView1.Rows[i].Cells[0].Value.ToString();
                ExcelApp.Cells[i + 2, 2] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                ExcelApp.Cells[i + 2, 3] = dataGridView1.Rows[i].Cells[2].Value.ToString();
            }

            string datetime = DateTime.Now.ToString("MMddyyyyhmmtt");
            ExcelApp.ActiveWorkbook.SaveCopyAs("F:\\HousekeepingAndStaffReport" + datetime + ".xls");
            ExcelApp.ActiveWorkbook.Saved = true;
            ExcelApp.Quit();
            MessageBox.Show("Excel file created,you can find the file F:\\HousekeepingAndStaffReport.xls");
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            PrintDGV.Print_DataGridView(dataGridView1);
        }
    }
}
